#!/usr/bin/python3
# -*- coding: UTF-8 -*-

from pymongo import MongoClient

import config

# reportCateID = 6
# cn = 6

server = config.dbServer
database = config.db
sessionTable = config.editorSessionCollection
imageTable = config.imageCollection
specialImageTable = config.specialImageCollection
testLabel = config.testLabel

client = MongoClient(server)
db = client[database]
editorSessions = db[sessionTable]
validImages = db[imageTable]
specialImages = db[specialImageTable]


def build_report(category):
    reportName = config.reportDir + "/autoReport_" + testLabel + "_cate" + str(category) + ".html"
    if config.isDebug:
        reportName = config.reportDir + "/autoReport_" + testLabel + "_debug_cate" + str(category) + ".html"
    report = open(reportName, "w", encoding='utf-8')

    array = []
    array.append(
        '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> ')
    array.append(
        '<html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  <title>Document</title> </head>')
    array.append('<div>')
    array.append('<table border="1" width="800" >')

    report.writelines(array)
    count = 0

    # for cat in range(1, cateNum):
    # if reportStrategy == 1:
    # 	docs = editorSessions.find({ "isTestFlag":1, "acceptFlag": 1, "testLabel": testLabel, "articleCategory": cat })
    # elif reportStrategy == 2:
    # 	docs = editorSessions.find({ "isTestFlag":1, "$or":[ {"acceptFlag":1}, {"acceptFlag":-1}], "testLabel": testLabel, "articleCategory": cat })
    # else:

    docs = editorSessions.find({"testLabel": testLabel, "articleCategory": category})

    for doc in docs:
        print(len(doc['recommendedImgs']))
        if len(doc['recommendedImgs']) >= 1:
            count += 1
            if config.isDebug and count not in config.debug[category]:
                continue
            line = []
            line.append('<div class="testDocs">')
            # header
            line.append('<tr>')
            line.append('<td>')
            # serial
            line.append('<p>')
            line.append('第' + str(count) + '篇')
            line.append('</p>')
            # title
            line.append('<p>《')
            line.append(doc['title'])
            line.append('》</p>')
            # keywords
            line.append('<p>')

            for i in range(len(doc['keywords'])):
                line.append(str(doc['keywords'][i]['keywords']))
                line.append("	")
                line.append(str(round(doc['keywords'][i]['score'], 1)))
                line.append("	")
            line.append('</p>')
            # content
            line.append('<p>')
            line.append(doc['content'])
            line.append('</p>')
            # images
            line.append('<p>')

            line.append('<table>')
            line.append('<tr>')
            for i in range(min(7, len(doc['recommendedImgs']))):
                image = validImages.find({"_id": doc['recommendedImgs'][i]})
                if (image.count() == 0):
                    image = specialImages.find({"_id": doc['recommendedImgs'][i]})
                line.append('<td>')
                line.append('<div>')
                line.append('<img src="')
                line.append(image[0]['imgOriginalURL'])
                line.append(' " height="250" width="300" />')
                line.append('</div>')
                line.append('<div style="font-size:20px">')
                line.append(str(doc['recImgs'][i]['imgScore']))
                line.append('</div>')
                # tagName
                line.append('<div style="font-size:15px">')
                for j in range(len(doc['recImgs'][i]['tagName'])):
                    line.append(str(doc['recImgs'][i]['tagName'][j]))
                    line.append("	")
                line.append('</div>')
                line.append('</td>')
                if (i + 1) % 3 == 0:
                    line.append('<br>')
            line.append('</tr>')
            line.append('</table>')
            line.append('</p>')
            # tail
            line.append('</td>')
            line.append('</tr>')

            line.append('</div>')
            report.writelines(line)

    array = []

    array.append('</table>')
    array.append('</div>')
    array.append('</html>')

    report.writelines(array)
    report.close()


if __name__ == '__main__':
    for cat in range(1, 7):
        build_report(cat)
